import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/login/login.vue";
import Home from "../views/home/index.vue";
import About from "../views/home/about.vue";
import Wellcome from "../views/wellcome/index.vue";
import Accountadd from "../views/account/Accountadd.vue";
import Accountlist from "../views/account/Accountlist.vue";
import Articlelist from "../views/article/Articlelist.vue";
import Articleadd from "../views/article/Articleadd.vue";
import Classifylist from "../views/classify/classifylist.vue";
import Classifyadd from "../views/classify/classifyadd.vue";
import Musicadd from "../views/music/musicadd.vue";
import Musiclist from "../views/music/musiclist.vue";
import Messagelist from "../views/message/messagelist.vue";
import Friendlinkadd from "../views/Friendlink/Friendlinkadd.vue";
import Friendlinklist from "../views/Friendlink/Friendlinklist.vue";
import Settings from "../views/about/index.vue";

Vue.use(VueRouter);

const routes = [
    {
      path: "/",
      name: "Login",
      meta: {
        isPass: true
      },
      component: Login
    },
    {
      path: "/home",
      name: "Home",
      component: Home,
      redirect: '/wellcome',
      children: [{
        path: "/wellcome",
        name: "Wellcome",
        meta: {
          title: '首页'
        },
        component: Wellcome,
      },
      {
        path: "/accountAdd",
        name: "Accountadd",
        meta: {
          title: '创建账号'
        },
        component: Accountadd
      },
      {
        path: "/accountEdit/:id",
        name: "Accountedit",
        props: true,
        meta: {
          title: '修改账号'
        },
        component: Accountadd
      },
      {
        path: "/accountList",
        name: "Accountlist",
        meta: {
          title: '账号列表'
        },
        component: Accountlist
      },
      {
        path: "/articleList",
        name: "Articlelist",
        meta: {
          title: '文章列表'
        },
        component: Articlelist
      },
      {
        path: "/articleEdit/:id",
        name: "Articleedit",
        meta: {
          title: '文章修改'
        },
        props: true,
        component: Articleadd
      },
      {
        path: "/articleAdd",
        name: "Articleadd",
        meta: {
          title: '创建文章'
        },
        component: Articleadd
      },
      {
        path: "/classifyList",
        name: "Classifylist",
        meta: {
          title: '分类列表'
        },
        component: Classifylist
      },
      {
        path: "/classifyAdd",
        meta: {
          title: '创建分类'
        },
        name: "Classifyadd",
        component: Classifyadd
      },
      {
        path: "/classEdit/:id",
        name: "Classifyedit",
        props: true,
        meta: {
          title: '修改分类'
        },
        component: Classifyadd
      },
      {
        path: "/musicAdd",
        name: "Musicadd",
        meta: {
          title: '新增音乐'
        },
        component: Musicadd
      },
      {
        path: "/musicEdit/:id",
        name: "Musicedit",
        props: true,
        meta: {
          title: '修改音乐'
        },
        component: Musicadd
      },
      {
        path: "/musicList",
        name: "Musiclist",
        meta: {
          title: '音乐列表'
        },
        component: Musiclist
      },
      {
        path: "/messageList",
        name: "Messagelist",
        meta: {
          title: '留言列表'
        },
        component: Messagelist
      },
      {
        path: "/friendlinkAdd",
        name: "Friendlinkadd",
        meta: {
          title: '创建友链'
        },
        component: Friendlinkadd
      },
      {
        path: "/friendlinkEdit/:id",
        name: "Friendlinkedit",
        props: true,
        meta: {
          title: '编辑友链'
        },
        component: Friendlinkadd
      },
      {
        path: "/friendlinkList",
        name: "Friendlinklist",
        meta: {
          title: '友链列表'
        },
        component: Friendlinklist
      },
        {
          path: "/Settings",
          name: 'Settings',
          meta: {
            title: '系统设置'
          },
          component: Settings
        },
      {
        path: '/about',
        name: 'About',
        meta: {
          title: '关于'
        },
        component: About
      }
    ]
  }
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes
});
// 挂载路由导航守卫
router.beforeEach((to, form, next) => {
  // to 将要访问的路径
  // form 表示从那个路径跳转来的
  // next是一个函数 表示放行 next（）放行 next（'/login'）强制跳转
  // 获取token
  const tokenStr = window.localStorage.getItem('token')
  if (to.path === '/') {
    // 如果访问登录页面 已经登录则跳转到主页
    if (tokenStr) {
      next()
    }
    return next()
  } else {
    // 如果访问非登录页面 未登录则跳转到登录页
    if (!tokenStr) {
      if (to.meta.isPass) {
        next()
      } else {
        next({ path: '/' })
      }
    } else {
      next()
    }
  }
})

export default router;
